package com.yhd.hmily.service;

import com.yhd.hmily.dao.AccountInfoDao;
import com.yhd.hmily.feign.Bank2Client;
import org.dromara.hmily.annotation.Hmily;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AccountInfoService {


	@Autowired
	private AccountInfoDao accountInfoDao;


	@Resource
	private Bank2Client bank2Client;

	public void updateAccountBalance(String accountNo, Double amount) {
		System.out.println("******** Bank1 Service Begin ... " );
		accountInfoDao.updateAccountBalance(accountNo ,amount* -1);//转出
		this.prepare(accountNo,amount);
	}





	@Hmily(confirmMethod = "commit", cancelMethod = "rollback")
	public void prepare(String accountNo, double amount) {
		System.out.println("******** Bank1 Service  prepare...  " );
		if(!bank2Client.transfer(amount)){
			throw new RuntimeException("bank2 exception");
		}
	}


	public void commit( String accountNo, double amount) {
		System.out.println("******** Bank1 Service commit..." );
	}


	public void rollback( String accountNo, double amount) {
		accountInfoDao.updateAccountBalance(accountNo ,amount );
		System.out.println("******** Bank1 Service rollback...  " );
	}


}
